import { getMvComemnt, requestSerachHot, requestSerachWord, songDetail, getMvSrc, getMvDetail, getSimilarMv } from "@/api/index"
import { defineStore } from 'pinia'
import { homeStore } from '@/store/home.js'
import { musicplaycontrolStore } from '@/store/playcontrol.js'

//注意,defineStore的名字必须是唯一的
export const search = defineStore('search', {
  state: () => {
    return {
      searchhotlist: [],
      searchSonglist: [],
      mvSrc: '',
      mvDetail: {
        desc: '', name: '', artistName: ''
      },
      similarMvs: [],
      // mv评论
      mvComment: []
    }
  },
  actions: {

    //获取热搜榜
    async asyncRequestSerachHot() {
      let res = await requestSerachHot()
      if (res.status === 200) {
        this.searchhotlist = res.data.data
      }
    },
    //获取关键字搜索
    async asyncrequestSerachWord(keyword) {
      let res = await requestSerachWord(keyword)
      if (res.data.result && res.data.result.songs && res.data.result.songs.length != 0) {
        this.searchSonglist = res.data.result.songs
        homeStore().playlist = res.data.result.songs
      } else {
        console.log('search 仓库报错');
      }
    },
    // 搜索的时候更换列表
    async asyncsongDetail(id, item, index) {
      let res = await songDetail(id)
      //查找歌词和名字
      //置换当前播放歌曲
      homeStore().singlesong = res.data.songs[0]
      musicplaycontrolStore().asyncrequestLyric(id)
      homeStore().playCurrentIndex = index
      musicplaycontrolStore().pause = false
    },
    // 获取mvsrc
    async asyncgetMvSrc(mvid) {
      let res = await getMvSrc(mvid)
      this.mvSrc = res.data.data.url
    },
    // 获取mv详情
    async asyncgetMvDetail(mvid) {
      let res = await getMvDetail(mvid)
      // console.log('mv', res);
      // this.mvSrc = res.data.data.url
      // console.log('mv详情', res);
      this.mvDetail.artistName = res.data.data.artistName
      this.mvDetail.name = res.data.data.name
      this.mvDetail.desc = res.data.data.desc
      this.mvDetail.cover = res.data.data.cover
      this.mvDetail.subCount = res.data.data.subCount
      this.mvDetail.shareCount = res.data.data.shareCount
      this.mvDetail.commentCount = res.data.data.commentCount
      // console.log('详情1', res.data);
    },
    async asyncgetSimilarMv(mvid) {
      let res = await getSimilarMv(mvid)
      this.similarMvs = res.data.mvs
      console.log('相似mv322', this.similarMvs);
    },
    // 获取mv评论
    async asyncgetMvComemnt(mvid) {
      let res = await getMvComemnt(mvid)
      this.mvComment = res.data.comments
      console.log(this.mvComment);
    }
  },
  getters: {
  }
})
